// index.js
var api = require("../../api.js"),
  app = getApp();
var WxParse = require("../../wxParse/wxParse.js");
var r;
Page({
  data: {
    couponData: [],
    couponNo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    r = this;
    r.setData({
      partnerId: options.partnerId
    })
  },
  onShow:function(){
    var that = this;
    r.toast = that.selectComponent("#toast");
   },  
   /**
   * 生命周期函数--监听页面初次渲染完成
   */
  submitForm: function (e) {
    var form = e.detail.value;
    var that = this;
    var t = new Object();
    var user = getApp().globalData.wxUserInfo;
    t = {

      perName: form.perName,
      perPhone: form.perPhone,
      perJob: form.perJob,
      parId: r.data.partnerId,
    };
    console.log(t);
    if (t.perName == null || t.perName == "") {
      r.tips.showTopTip('名字不能为空');
      return;
    }
    if (t.perPhone == null || t.perPhone == "") {
      r.tips.showTopTip('手机号为空');
      return;
    } else {
      if (!/^1[34578]\d{9}$/.test(e.detail.value.perPhone)) {
        r.tips.showTopTip('手机号格式错误');
        return false;
      }
    }
    if (t.perJob == null || t.perJob == "") {
      r.tips.showTopTip('职位不能为空');
      return;
    }
    wx.request({
      url: api.mine.addPerson,
      data: t,
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.statusCode == 200) {
          console.log(res)
          if (res.data.code == 200) {
            wx.showToast({
              title: '添加成功'
            })
            setTimeout(function(){
              var pages = getCurrentPages(); //获取页面栈
            if (pages.length > 1) {
              //上一个页面实例对象
              var prePage = pages[pages.length - 2];
              //调用上一个页面的onShow方法
              prePage.onLoad();
              wx.navigateBack({
                delta: 1
              })
            }
            },2000)
          } else {
            r.toast.showToast(res.data.msg);

          }
        }

      }
    })
  },

  listenerNewLabelInput: function (e) {
    r.data.couponNo = e.detail.value;
  },

  changeNum(e) {
    console.log(13212)
    let couponNo = r.data.couponNo;
    if (r.data.couponNo == '') {
      r.tips.showTopTip('请输入优惠卷码');
      return;

    } else {
      var t = new Object();
      t.couponNo = r.data.couponNo, t.openId = getApp().globalData.wxUserInfo.openId;
      wx.request({
        url: api.mine.checkCoupon,
        data: t,
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          if (res.statusCode == 200) {
            console.log(res)
            if (res.data.code == 200) {
              r.initData();
            } else {
              r.toast.showToast(res.data.msg);

            }
          }

        }
      })
    }

  },

  //返回
  return(){
    var pages = getCurrentPages(); //获取页面栈
    if (pages.length > 1) {
      //上一个页面实例对象
      var prePage = pages[pages.length - 2];
      //调用上一个页面的onShow方法
      // prePage.onLoad();
      wx.navigateBack({
        delta: 1
      })
    }
  },
})